package com.pospi.pospihold.http;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.pospihold.App;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.dto.OrderBeen;
import com.pospi.pospihold.dto.OrderDto;
import com.pospi.pospihold.dto.PayBeen;
import com.pospi.pospihold.dto.ReturnDto;
import com.pospi.pospihold.util.Api;
import com.pospi.pospihold.util.Constant;
import com.pospi.pospihold.util.GetData;
import com.pospi.pospihold.util.OrderNoUtil;
import com.pospi.pospihold.util.SaveOrderInfo;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Qiyan on 2016/6/24.
 */
public class UploadData {
    public static final String UP_SUCCESS = "1";
    public static final String UP_FAILED = "2";


    /**
     * 上传订单到服务器
     */
    public static RequestParams getUpData(Context context, OrderBeen dto) {
        //得到店铺的Id
        SharedPreferences preferences = context.getSharedPreferences("StoreMessage", Context.MODE_PRIVATE);

        String Shopid = preferences.getString("Id", "");//得到店铺的ID
        preferences = context.getSharedPreferences("Token", Context.MODE_PRIVATE);
        String value = preferences.getString("value", "");
        String[] names = value.split("\\,");
        String deviceNo = names[2];//收银机号
        String uid = names[1];
        String sid = GetData.getYYMMDDHHMMSSNoSpellingTime() + GetData.getStringRandom(20);//得到一个32位的随机数
        String cashierNumber = App.getCashierMsgDto().getNumber();
        JSONObject orderObject = new JSONObject();
        JSONArray headArray = new JSONArray();

        JSONObject head = new JSONObject();
        try {
            head.put("obj1", "");
            head.put("paxNumber", 0);
            head.put("obj6", "");//
            head.put("remoney", dto.getAmount());
            head.put("cashNo", dto.getDeviceNo());//收银机号
            head.put("obj10", "");
            head.put("number", 1);
            head.put("type", 1);
            head.put("obj4", "");
            head.put("acmoney", dto.getAmount());
            head.put("obj9", "");
            head.put("date", dto.getDate());
            head.put("imel", "POSPi-uuid-3E57B975-9855-49AB-804F-9AF0D9015558");
            head.put("saleMethod", 0);
            head.put("obj2", "");
            head.put("obj7", "");
            head.put("sid", dto.getSid());
            head.put("no", dto.getOrderNo());
            head.put("uid", dto.getUid());
            head.put("discount", dto.getDiscount());
            head.put("obj5", "");
            head.put("cgmoney", dto.getAmount());
            head.put("by", dto.getCashierNo());//收银员号
            head.put("shopNo", dto.getShopNo());
            head.put("obj3", "");
            head.put("status", 4);
            head.put("obj8", "");
            headArray.put(head);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        List<GoodsDto> goodsDtos = SaveOrderInfo.changeToList(dto.getGoodsDetail());
        JSONArray detail = new JSONArray();
        for (GoodsDto goodsDto : goodsDtos) {
            JSONObject object = new JSONObject();
            try {
                object.put("obj1", "");
                object.put("createTime", dto.getDate()); //当前的时间
                object.put("discountType", 0);//
                object.put("order_sid", dto.getSid());
                object.put("parentSid", goodsDto.getSid());
                object.put("type", 1);//订单的类型 1是销售，2是退货
                object.put("saleMethod", 0);//
                object.put("totalDiscount", "0.00");
                object.put("number", 1);//数量
                object.put("m_price", goodsDto.getPrice());//单价
                object.put("sid", goodsDto.getSid());//
                object.put("price", "20.00");//
                object.put("Uid", dto.getUid());//
                object.put("menus_sid", goodsDto.getSid());//
                object.put("callCount", 0);//订单号
                object.put("menus_name", goodsDto.getName());//
                object.put("saleprice", goodsDto.getNum() * goodsDto.getPrice());//
                object.put("obj3", 0);//
                object.put("lineNum", 1);//
                object.put("status", 4);//
                object.put("singleDiscount", "0.00");//
                detail.put(object);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        PayBeen payBeen = new Gson().fromJson(dto.getPayDetail(), PayBeen.class);

        JSONArray array = new JSONArray();
        try {
            JSONObject object = new JSONObject();
            object.put("obj3", "");
            object.put("price_ys", dto.getAmount());
            object.put("orderBy", "");
            object.put("lineNum", 1);
            object.put("payId", payBeen.getSid());
            object.put("obj2", "0");
            object.put("payType", payBeen.getPayType1());
            object.put("obj5", "");
            object.put("price", dto.getAmount());
            object.put("sid", payBeen.getSid());
            object.put("order_sid", dto.getSid());
            object.put("name", payBeen.getName());
            object.put("Uid", payBeen.getUId());
            object.put("obj4", "");
            object.put("price_zl", "0.00");
            object.put("status", 1);
            array.put(object);

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            orderObject.put("Order", headArray);
            orderObject.put("Order_Menus", detail);
            orderObject.put("Order_PayType", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestParams params = new RequestParams();//实例化后存入键值对
        params.put("Order", headArray.toString());
        params.put("Order_Menus", detail.toString());
        params.put("Order_PayType", array.toString());
        Log.i("order", params.toString());

        return params;

//        LoadServer.getConnect(context, Api.SYNCORDER, params, new AsyncHttpResponseHandler() {
//            @Override
//            public void onSuccess(int i, Header[] headers, byte[] bytes) {
//                Log.i("上传订单成功", new String(bytes));
//                if (i == 200) {
//                    Log.i("向服务器发送数据成功", new String(bytes));
//                    Log.i("向服务器上传订单", "上传订单");
//
//                    try {
//                        JSONObject object = new JSONObject(new String(bytes));
//                        ReturnDto returnDto = new ReturnDto();
//                        returnDto.setResult(object.getInt("Result"));
//                        returnDto.setMessage(object.getString("Message"));
//                        returnDto.setValue(String.valueOf(object.getString("Value")));
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//            }
//
//            @Override
//            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
//                Log.i("上传订单失败", new String(bytes));
//            }
//        });

    }
    public static RequestParams getParams(Context context,OrderDto dto){
        //得到店铺的Id
        SharedPreferences preferences = context.getSharedPreferences("StoreMessage", Context.MODE_PRIVATE);

        String Shopid = preferences.getString("Id", "");//得到店铺的ID
        preferences = context.getSharedPreferences("Token", Context.MODE_PRIVATE);
        String value = preferences.getString("value", "");
        String[] names = value.split("\\,");
        String deviceNo = names[2];//收银机号
        String uid = names[1];

        String cashierNumber = App.getCashierMsgDto().getNumber();
        String no = GetData.getYYMMDDHHMMSSNoSpellingTime()+GetData.getStringRandom(18);
        RequestParams params = new RequestParams();
        JSONObject head = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            head.put("cashNo", "100112");
            head.put("cashierCode", cashierNumber);
            head.put("cashierName", "");
            head.put("change", "");
            head.put("discountAmount", "");
            head.put("hhmmss", GetData.getHHmmssTimet());
            head.put("imel", "");
            head.put("isRefund", "");
            head.put("liushuiNo", "");
            head.put("no", "");

            head.put("number", "");
            head.put("paxNumber", "");
            head.put("remake", "");
            head.put("saleAmount", dto.getYs_money());
            head.put("saleMethod", "");
            head.put("shopNo", Shopid);

            head.put("sid", no);
            head.put("status", "");
            head.put("tableNo", dto.getTable());
            head.put("totalAmount", dto.getYs_money());
            head.put("totalNumber", "");
            head.put("type", "");

            head.put("uid", uid);
            head.put("up", "1");
            head.put("xph", "");
            head.put("yyyymmdd", GetData.getYYMMDDTime());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        List<GoodsDto> goodsDtos = SaveOrderInfo.changeToList(dto.getOrder_info());
        for (GoodsDto goodsDto : goodsDtos) {


            JSONObject object = new JSONObject();
            try {
                object.put("sid",no);
                object.put("type","1");
                object.put("status","");
                object.put("totalAmount",goodsDto.getPrice()*goodsDto.getNum());
                object.put("uid",uid);
                object.put("yyyymmdd",GetData.getYYMMDDTime());
                object.put("saleAmount",goodsDto.getPrice()*goodsDto.getNum());
                object.put("saleMethod","");
                object.put("obj1","");
                object.put("obj2","");
                object.put("obj3","");
                object.put("obj4",goodsDto.getMainPrinterSid());
                object.put("obj5","");
                object.put("menus_name",goodsDto.getName());

                object.put("menus_sid",goodsDto.getSid());
                object.put("m_price",goodsDto.getPrice());
                object.put("lineNum","");
                object.put("hhmmss",GetData.getHHmmssTimet());
                object.put("number", goodsDto.getNum());
                array.put(object);



            } catch (JSONException e) {
                e.printStackTrace();
            }

            String label = goodsDto.getLabel();
            if (label != null && !label.isEmpty()) {
                String[] strings = label.split(" ");
                for (String s : strings) {
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("sid",no);
                        jsonObject.put("type","1");
                        jsonObject.put("status","");
                        jsonObject.put("totalAmount","0");
                        jsonObject.put("uid",uid);
                        jsonObject.put("yyyymmdd",GetData.getYYMMDDTime());
                        jsonObject.put("saleAmount","0");
                        jsonObject.put("saleMethod","");
                        jsonObject.put("obj1","");
                        jsonObject.put("obj2","");
                        jsonObject.put("obj3","");
                        jsonObject.put("obj4",goodsDto.getMainPrinterSid());
                        jsonObject.put("obj5","");
                        jsonObject.put("menus_name","-"+s);

                        jsonObject.put("menus_sid",goodsDto.getSid());
                        jsonObject.put("m_price",goodsDto.getPrice());
                        jsonObject.put("goodsParentSid","");
                        jsonObject.put("hhmmss",GetData.getHHmmssTimet());
                        jsonObject.put("number", "1");
                        array.put(jsonObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

        }



        params.put("detail", array.toString());
        params.put("master", head.toString());
        Log.i("data", params.toString());
        return params;

    }

}
